﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using Library_dll;
using Library_dll.Entities;
using System.IO;
using System.Xml.Serialization;


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {

        Radar Rad = new Radar();
        RadarStatus RadStat = new RadarStatus();
        int checkCount = 1;
        int TargetName = 0;
        public Form1()
        {
            InitializeComponent();
            lblRadarName.Text = "Our Radar is Better then yours 5000";
            XmlTextReader Inital_Radar_Reader;
            #region Initialize_Radar
            try
            {
                Inital_Radar_Reader = new XmlTextReader("Initial_Radar.xml");
                Inital_Radar_Reader.Read();
                while (!Inital_Radar_Reader.EOF)
                {
                    Inital_Radar_Reader.Read();
                    if (!Inital_Radar_Reader.IsStartElement("Radar")) break;
                    Inital_Radar_Reader.Read();
                    lblRadarName.Text = Inital_Radar_Reader.ReadElementString("RadarName");
                }
            }
            catch (Exception ex)
            {
                StreamWriter tw = File.AppendText("Exceptions.txt");
                tw.WriteLine(DateTime.Now + ":" + ex);
                tw.Close();
            }
            #endregion
            //intialize timer, have it query xml files to see if it should update its stupid face.
        }

        private void btnRandom_Click(object sender, EventArgs e)
        {
            //Generates numbers for all of the elements
            //insert case statements for bird/mis/plane later
            int SpeedRnd;
            int AltRnd;
            int DirRnd;
            int DisRnd;
            if (TargetName == 1)
            {
                SpeedRnd = Utility.RandomNumber(1, 120);
                AltRnd = Utility.RandomNumber(1000, 5000);
                DirRnd = Utility.RandomNumber(0, 359);
                DisRnd = Utility.RandomNumber(800, 2000);
            }
            else if (TargetName == 2)
            {
                SpeedRnd = Utility.RandomNumber(1, 5000);
                AltRnd = Utility.RandomNumber(1000, 40000);
                DirRnd = Utility.RandomNumber(0, 359);
                DisRnd = Utility.RandomNumber(800, 2000);
            }
            else
            {
                SpeedRnd = Utility.RandomNumber(1, 2000);
                AltRnd = Utility.RandomNumber(1000, 40000);
                DirRnd = Utility.RandomNumber(0, 359);
                DisRnd = Utility.RandomNumber(800, 2000);
            }
            
            Rad.Speed = SpeedRnd;
            Rad.Altitude = AltRnd;
            Rad.Direction = DirRnd;
            Rad.Distance = DisRnd;
            checkCount = 1;
            UpdateDisplay(checkCount);

        }

        private void btnLock_Click(object sender, EventArgs e)
        {
            //Send XML file to Missile, allows missile to fire.
            //RadarXML.WriteXML(Rad);
            Rad.Firing_Status = 0;
            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(Radar));
                TextWriter textWriter = new StreamWriter("../../../../Radar.xml");
                serializer.Serialize(textWriter, Rad);
                textWriter.Close();
            }
            catch (Exception ex)
            {
                //Write ex to Log
            }
            btnRandom.Enabled = false;
        }
        #region RadioButtons
        private void radBird_CheckedChanged(object sender, EventArgs e)
        {
            Rad.Threatlevel = 1;
            Rad.TargetName1 = "Birds";
            txtThreat.Text = Convert.ToString(Rad.Threatlevel);
            txtName.Text = "Bird";
            btnRandom.Enabled = true;
            checkCount = 0;
            UpdateDisplay(checkCount);
            btnLock.Enabled = true;
            TargetName = 1;
        }

        private void radMissle_CheckedChanged(object sender, EventArgs e)
        {
            Rad.Threatlevel = 5;
            Rad.TargetName1 = "Missile";
            txtThreat.Text = Convert.ToString(Rad.Threatlevel);
            txtName.Text = "Missile";
            btnRandom.Enabled = true;
            checkCount = 0;
            UpdateDisplay(checkCount);
            btnLock.Enabled = true;
            TargetName = 2;
        }

        private void radNuns_CheckedChanged(object sender, EventArgs e)
        {
            Rad.Threatlevel = 3;
            Rad.TargetName1 = "Plane";
            txtThreat.Text = Convert.ToString(Rad.Threatlevel);
            txtName.Text = "Planes";
            btnRandom.Enabled = true;
            checkCount = 0;
            UpdateDisplay(checkCount);
            btnLock.Enabled = true;
            TargetName = 3;
        }

        private void radNone_CheckedChanged(object sender, EventArgs e)
        {
            Rad.Threatlevel = 0;
            txtThreat.Text = Convert.ToString(Rad.Threatlevel);
            txtName.Text = "None";
            checkCount = 0;
            UpdateDisplay(checkCount);
            btnLock.Enabled = false;
            btnRandom.Enabled = false;
        }
        #endregion 
        private void UpdateDisplay(int RadioCheck)
        {
            if (RadioCheck == 0)
            {
                Rad.Distance = 0;
                Rad.Altitude = 0;
                Rad.Direction = 0;
                Rad.Speed = 0;
            }
            txtDistance.Text = Convert.ToString(Rad.Distance);
            txtAltitude.Text = Convert.ToString(Rad.Altitude);
            txtDirection.Text = Convert.ToString(Rad.Direction); 
            txtSpeed.Text = Convert.ToString(Rad.Speed);
        }

        private void quitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void TmrRadarStatus_Tick(object sender, EventArgs e)
        {
            RadStat.RadarStatus1 = 1;
            try
            {
                XmlSerializer serializer1 = new XmlSerializer(typeof(RadarStatus));
                TextWriter textWriter1 = new StreamWriter("../../../../RadStatus.xml");
                serializer1.Serialize(textWriter1, RadStat);
                textWriter1.Close();
            }
            catch (Exception ex)
            {
                //meh
            }
        }
    }
}
